package com.moorror.codegenvel.view.window.codegen.view;

import com.google.common.collect.Lists;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.OnePixelSplitter;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.ListTableModel;
import com.moorror.codegenvel.common.dialog.DialogFormMark;
import com.moorror.codegenvel.common.table.TableAddNewRowAction;
import com.moorror.codegenvel.common.table.TreeLineObject;
import com.moorror.codegenvel.common.table.TreeLineWrap;
import com.moorror.codegenvel.view.window.codegen.domain.ExtractFileDesc;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import javax.swing.*;
import java.util.List;
import java.util.function.Function;

/**
 * @Author jiyanghuang
 * @Date 2025/9/30 22:19
 */
public class DialogFormExtractVelocityTemplate implements DialogFormMark {

    private Project project;

    private ToolbarDecorator decorator;

    private JBTable tableView;

    private JPanel tablePanel;

    private Function<ExtractFileDesc, Pair<Boolean, String>> doUpdate;

    private String dialogTitle;

    private JPanelExtractVelocityTemplate panelTemplate;

    private OnePixelSplitter splitter;

    /**
     * @param project
     * @param dialogTitle
     * @param clazz       一行由哪个类来描述
     * @param tableData   数据List
     * @param doUpdate    关闭时，重新更新整个List
     */
    public DialogFormExtractVelocityTemplate(Project project, String dialogTitle, Class<? extends TreeLineObject> clazz, List<? extends TreeLineObject> tableData, Function<ExtractFileDesc, Pair<Boolean, String>> doUpdate) {
        this.project = project;
        this.dialogTitle = dialogTitle;
        tableView = TreeLineWrap.tableView(clazz, tableData, project);
        ListTableModel listTableModel = (ListTableModel) tableView.getModel();
        decorator = ToolbarDecorator.createDecorator(tableView);
        TableAddNewRowAction addLineAction = new TableAddNewRowAction(clazz, listTableModel);
        decorator.setAddAction(addLineAction);
        tablePanel = decorator.createPanel();

        panelTemplate = new JPanelExtractVelocityTemplate(project, this);
        splitter = new OnePixelSplitter(0.5f);
        splitter.setFirstComponent(tablePanel);
        splitter.setSecondComponent(panelTemplate);

        this.doUpdate = doUpdate;
    }

    @Override
    public JComponent jComponent() {
        return splitter;
    }

    @Override
    public String title() {
        if (StringUtils.isBlank(dialogTitle)) {
            return "Editor Table";
        }
        return dialogTitle;
    }

    @Override
    public Function<DialogFormExtractVelocityTemplate, Pair<Boolean, String>> okFun() {
        return (form) -> {
            if(StringUtils.isBlank(panelTemplate.templateName())){
                return Pair.of(false, "Template Name Can Not Empty");
            }
            if(null != this.doUpdate){
                /**
                 * 保存时，获取最新的值
                 */
                panelTemplate.fetchFromPanel();
                return this.doUpdate.apply(panelTemplate.getExtractFileDesc());
            }
            return Pair.of(true, null);
        };
    }

    public <T extends TreeLineObject> List<T> fetchTableValue() {
        if (null != tableView) {
            ListTableModel listTableModel = (ListTableModel) tableView.getModel();
            List values = TreeLineWrap.lastValue((listTableModel.getItems()));
            return values;
        }
        return Lists.newArrayList();
    }

    public void replaceFast(Project project, VirtualFile virtualFile){
        panelTemplate.updateReplaceFast(project, virtualFile);
    }
}
